1156C - Match Points - CodeForces Solution


binary search greedy sortings ternary search two pointers *2000

Please click on ads to support us..

Python Code:

def solve_one_hypothesis(n, z, x, k):
    for i in range(k):
                if x[n - k + i] - x[i] < z:
            return False
    return True


def my_fn(n, z, x):

    total = 0
    visited = [False for _ in range(len(x))]

    x = sorted(x)

    left = 0
    right = n // 2

    while (right - left) > 5:
        m1 = left + int((right - left) // 2)
                if solve_one_hypothesis(n, z, x, m1):
            left = m1
        else:
            right = m1

            for k in list(range(left, right + 1))[::-1]:
        if solve_one_hypothesis(n, z, x, k):
            return k


s1 = input()
s2 = input()

n, z = [int(v) for v in s1.split(' ')]
x = [int(v) for v in s2.split(' ')]

total = my_fn(n, z, x)
print(total)

C++ Code:

#include<bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
using namespace std;
typedef long long ll;

int main()
{
    FIO
    int n,z,c=0;
    cin>>n>>z;
    int a[n];
    for(int i=0;i<n;i++)cin>>a[i];
    sort(a,a+n);
    ll mid = n/2;
    ll x=mid;
    for(int i=0;i<mid&&x<n;)
    {
        if(abs(a[i]-a[x])>=z)
        {
            c++;
            x++;
            i++;
        }
        else x++;
    }
    cout<<c;

    return 0;
}


Comments

Submit
0 Comments
More Questions

479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game